﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Survey.Data.Interfaces;
using System.Linq.Expressions;

namespace Survey.Data
{
    public abstract class AbstractCrudDao<T> : IBaseCrudDao<T> where T : class
    {
        public T Entity;
        
        private SurveyContext entidades = new SurveyContext();

        public void Add(T pEntity)
        {
            entidades.AddObject(pEntity.GetType().Name + "Set", pEntity);
        }
        public void Delete(T pEntity)
        {
            entidades.DeleteObject(pEntity);
        }
        public void Attach(T pEntity)
        {
            entidades.AttachTo(pEntity.GetType().Name, pEntity);
        }
        public void Detach(T pEntity)
        {
            entidades.Detach(pEntity);
        }
        public void Update(T pEntity)
        {
            entidades.ApplyCurrentValues<T>(pEntity.GetType().Name, pEntity);
        }
        public IQueryable<T> Find(Expression<Func<T, bool>> where)
        {
            return entidades.CreateObjectSet<T>().Where(where);
        }
        public IQueryable<T> GetAll()
        {
            return entidades.CreateObjectSet<T>();
        }
        public void SaveChanges()
        {
            entidades.SaveChanges();
        }
    }
}